package eyl;

import robocode.Robot;
import java.awt.Color;
import robocode.HitRobotEvent;
import robocode.ScannedRobotEvent;

/**
 * Creates a robot that travels in a diamond pattern, starting from the top of the field,
 * and firing when an enemy is found.
 * . 
 * @author Edwin Lim
 * @version 1.1
 *
 */
public class CounterBots extends Robot {
  double midWidth, fWidth;
  double foward, shortAngle, oppositeAngle;

  /**
   * Creates and sets the properties of the robot and calculates the movements of the robot.
   */
  public void run() {
    // Set colors
    setBodyColor(Color.black);
    setGunColor(Color.blue);
    setRadarColor(Color.black);
    setBulletColor(Color.white);
    setScanColor(Color.blue);

    // Find the dimension of the field
    midWidth = getBattleFieldWidth() / 2;  
    fWidth = midWidth - getX();
    // Go to initial position
    turnLeft(getHeading());
    ahead( (getBattleFieldHeight()) - getY() - 100);
    turnRight(90); 
    ahead(fWidth);

    // Find the angle of the tangent of the first quadrant
    shortAngle = turnAngle(getBattleFieldWidth() / 2, getBattleFieldHeight() / 2);
    oppositeAngle = 180 - shortAngle - 90; 
    // Find the tangent of the first quadrant
    foward = ((getBattleFieldWidth() - 200) * (getBattleFieldWidth() - 200)) + 
    ((getBattleFieldHeight() - 200) * (getBattleFieldHeight() - 200));
    foward = (Math.sqrt(foward)) / 2;
    turnGunRight(90);
    // Keep moving in a diamond pattern
    while (true) {
      // Find which side has the short angle and 
      // which side is the opposite angle
      if ((getX() > (midWidth - 100) && (getX() < midWidth + 100))) {
        if (getY() > getBattleFieldHeight() - 200) {
          // top of the diamond
          turnLeft(getHeading() - 90);
          turnRight(shortAngle);
        }
        else {
          // bottom of the diamond
          turnRight(shortAngle + shortAngle);
        }
      }
      else {
          turnRight(oppositeAngle + oppositeAngle);
      } 
      ahead(foward);
    }

  }

  /**
   * Fires when an enemy robot is detected. 
   * 
   * @param e is the robot that was scanned.
   */
  public void onScannedRobot(ScannedRobotEvent e) {
    fire(2);
  }

  /**
   * Reset the robot to the initial settings when it collides with the wall.
   * 
   * @param e is the robot that was scanned.
   */
  public void onHitWall(HitRobotEvent e) {
    // Find the dimension of the field
    midWidth = getBattleFieldWidth() / 2;  
    fWidth = midWidth - getX();
    // Go to initial position
    turnLeft(getHeading() - 90);
    ahead(fWidth);
    turnLeft(90); 
    ahead( (getBattleFieldHeight()) - getY() - 100);
  }

  /**
   * Determines the angle in which the robot has to turn to find the hypotenuse. 
   * 
   * @param battleFieldWidth is the width of the map
   * @param battleFieldHeight is the height of the map
   * @return angle
   */
  public double turnAngle(double battleFieldWidth, double battleFieldHeight) {
    double angle = 0;
    // returns the arc tangent of a value
    angle = Math.atan(battleFieldHeight / battleFieldWidth);
    // returns the angle measured in radians to an approximate degree
    angle = Math.toDegrees(angle);
    return angle;
  }

}
